import 'package:flutter/material.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:oktoast/oktoast.dart';
import '../net/api/api_url.dart';
import '../net/http.dart';
import '../widget/image_asset_widget.dart';
import 'colors.dart';

class SuspendAccountDialog extends StatelessWidget {
  // SuspendAccountDialog.show(
  //   Get.context!,
  //   phone: userTel.value.toString(),
  //   time: result.data!.cancelTime.toString(),
  //   onUpgrade: () {},
  // );
  final String time;
  final String phone;
  final VoidCallback onUpgrade;

  const SuspendAccountDialog({
    Key? key,
    required this.time,
    required this.phone,
    required this.onUpgrade,
  }) : super(key: key);

  static Future<void> show(
    BuildContext context, {
    required String time,
    required String phone,
    required VoidCallback onUpgrade,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder:
          (_) => SuspendAccountDialog(
            phone: phone,
            time: time,
            onUpgrade: onUpgrade,
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      child: Dialog(
        insetPadding: EdgeInsets.zero,
        backgroundColor: Colors.transparent,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Stack(
              alignment: Alignment.topCenter,
              children: [
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    hiSpace(height: 24.px),
                    ConstrainedBox(
                      constraints: const BoxConstraints(minHeight: 220),
                      child: Container(
                        margin: EdgeInsets.only(
                          left: 16.px,
                          right: 16.px,
                          bottom: 6.px,
                        ),
                        decoration: BoxDecoration(
                          border: Border.all(color: AppColors.borderGreenD6),
                          gradient: const LinearGradient(
                            colors: [Colors.white, Colors.white],
                          ),
                          borderRadius: BorderRadius.circular(12),
                        ),
                        child: Column(
                          children: [
                            hiSpace(height: 32.px),
                            Text(
                              "账号申请注销中",
                              style: TextStyle(
                                fontSize: 18.px,
                                color: const Color(0xFF3A634F),
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            hiSpace(height: 34.px),
                            RichText(
                              text: TextSpan(
                                children: <TextSpan>[
                                  TextSpan(
                                    text: '可在',
                                    style: TextStyle(
                                      color: Color(0xff000000),
                                      fontSize: 16.px,
                                    ),
                                  ),
                                  TextSpan(
                                    text: '${time}',
                                    style: TextStyle(
                                      color: Color(0xFFEA0000),
                                      fontSize: 16.px,
                                    ),
                                  ),
                                  TextSpan(
                                    text: '前随时取消注销',
                                    style: TextStyle(
                                      color: Color(0xff000000),
                                      fontSize: 16.px,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            hiSpace(height: 43.px),
                            Container(
                              margin: EdgeInsets.symmetric(horizontal: 30.px),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  TextImgWithGradient(
                                    fontFamily: 'CustomFont',
                                    startColor: Color(0xFF3A634F),
                                    endColor: Color(0xFF3A634F),
                                    text: "取消注销",
                                    imWidth: 120.px,
                                    imHeight: 40.px,
                                    imgUrl:
                                        'static/images/common_btn_cancel.png',
                                    onPressed: () {
                                      http.netFetch(
                                        ApiUrl.cancelLogOff,
                                        method: Method.post,
                                        params: {'userAccount': phone},
                                        success: (data) {
                                          if (data.code == 200) {
                                            showToast("取消注销成功");
                                            onUpgrade.call();
                                            Navigator.of(context).pop();
                                          }
                                        },
                                      );
                                    },
                                    fontSize: 12.px,
                                  ),
                                  TextImgWithGradient(
                                    fontFamily: 'CustomFont',
                                    startColor: Color(0xFF234434),
                                    endColor: Color(0xFF234434),
                                    text: "关闭",
                                    imWidth: 120.px,
                                    imHeight: 40.px,
                                    onPressed: () {
                                      onUpgrade.call();
                                      Navigator.of(context).pop();
                                    },
                                    fontSize: 12.px,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                Positioned(
                  bottom: 0.px,
                  left: 0.px,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/leaf_left.png',
                    height: 48.px,
                    width: 70.px,
                  ),
                ),
                Positioned(
                  right: 0.px,
                  top: 0,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/leaf_right.png',
                    height: 48.px,
                    width: 82.px,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class TextImgWithGradient extends StatelessWidget {
  Color startColor, endColor;
  double fontSize;
  String text, fontFamily;
  String imgUrl;
  double imHeight; //图片宽高
  double imWidth; //图片宽高
  final VoidCallback? onPressed;
  TextImgWithGradient({
    Key? key,
    this.imgUrl = '',
    this.imHeight = 156,
    this.imWidth = 54,
    required this.onPressed,
    this.startColor = const Color(0xff000000),
    this.endColor = const Color(0xff000000),
    this.fontSize = 24,
    this.text = '',
    this.fontFamily = 'CustomFont',
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 先用TextPainter测量文本宽高
    final tp = TextPainter(
      text: TextSpan(
        text: text,
        style: TextStyle(fontSize: fontSize, fontFamily: fontFamily),
      ),
      textDirection: TextDirection.ltr,
    )..layout();
    final width = tp.width;
    final height = tp.height;

    final Gradient gradient = LinearGradient(
      colors: [startColor, endColor],
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
    );
    final Shader shader = gradient.createShader(
      Rect.fromLTWH(0, 0, width, height),
    );

    return GestureDetector(
      onTap: onPressed,
      child: Stack(
        alignment: Alignment.center,
        children: [
          imgUrl.isNotEmpty
              ? ImageAssetWidget(
                imageUrl: imgUrl,
                width: imWidth,
                height: imHeight,
              )
              : SizedBox(width: 0, height: 0),
          Column(
            children: [
              text.isNotEmpty
                  ? Text(
                    text,
                    style: TextStyle(
                      fontSize: fontSize,
                      fontFamily: fontFamily,
                      foreground: Paint()..shader = shader,
                    ),
                  )
                  : SizedBox(width: 0, height: 0),
            ],
          ),
        ],
      ),
    );
  }
}
